package com.jinzhi.jzweb.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.jinzhi.common.annotation.Log;
import com.jinzhi.common.base.BaseController;
import com.jinzhi.common.domain.Tree;
import com.jinzhi.common.utils.Result;
import com.jinzhi.jzweb.domain.CertificateProfileDO;
import com.jinzhi.jzweb.domain.KnowledgePointsDO;
import com.jinzhi.jzweb.service.CertificateNewsService;
import com.jinzhi.jzweb.service.CertificateProfileService;
import com.jinzhi.jzweb.service.KnowledgePointsService;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <pre>
 * 知识点
 * </pre>
 * <small> 2020-02-24 21:00:12 | lance</small>
 */
@Controller
@RequestMapping("/jzweb/knowledgePoints")
public class KnowledgePointsController extends BaseController {
    @Autowired
    private KnowledgePointsService knowledgePointsService;
    @Autowired
    private CertificateNewsService certificateNewsService;
    @Autowired
    private CertificateProfileService certificateProfileService;

    @GetMapping()
    @RequiresPermissions("jzweb:knowledgePoints:knowledgePoints")
    String KnowledgePoints() {
        return "jzweb/knowledgePoints/knowledgePoints";
    }

    @ResponseBody
    @GetMapping("/list")
    @RequiresPermissions("jzweb:knowledgePoints:knowledgePoints")
    public List<KnowledgePointsDO> list(KnowledgePointsDO knowledgePointsDTO) {
        String str = "";
        if (StringUtils.isNotBlank(knowledgePointsDTO.getProfileName())){
            Wrapper<CertificateProfileDO> cerWrapper = new EntityWrapper<CertificateProfileDO>().like("name",knowledgePointsDTO.getProfileName());
            List<CertificateProfileDO>  certificateProfileDOS = certificateProfileService.selectList(cerWrapper);
            for (CertificateProfileDO certificateProfileDO : certificateProfileDOS){
                str = str + certificateProfileDO.getId() + ",";
            }
        }else{
            str = "-1";
        }

        List<KnowledgePointsDO> pointsDOList;
        if (knowledgePointsDTO != null) {
            Map map = new HashMap();
			map.put("name", knowledgePointsDTO.getName());
            if(StringUtils.isNotBlank(str)){
                if(!str.equals("-1")){
                    map.put("profileId",str.substring(0,str.length()-1));
                }
            }else{
                map.put("profileId","");
            }
            pointsDOList = knowledgePointsService.selectListBy(map);
        } else {
            pointsDOList = knowledgePointsService.selectListAll();
        }
        return pointsDOList;
    }

    @GetMapping("/add")
    @RequiresPermissions("jzweb:knowledgePoints:add")
    String add(Model model, Long id, Long level, String name) {
        model.addAttribute("id", id);
        model.addAttribute("level", level + 1);
        model.addAttribute("name", name);
        model.addAttribute("certificateProfile", certificateProfileService.selByAll());
        return "jzweb/knowledgePoints/add";
    }

    @GetMapping("/edit")
    @RequiresPermissions("jzweb:knowledgePoints:edit")
    String edit(Long id, Model model) {
        KnowledgePointsDO knowledgePoints = knowledgePointsService.selectById(id);
        model.addAttribute("knowledgePoints", knowledgePoints);
        if (knowledgePoints != null) {
            if (knowledgePoints.getPid() != 0) {
                KnowledgePointsDO knowledgePointsDO = knowledgePointsService.selectById(knowledgePoints.getPid());
                model.addAttribute("pidName", knowledgePointsDO.getName());
            } else {
                model.addAttribute("pidName", "顶级节点");
            }
        } else {
            model.addAttribute("pidName", "顶级节点");
        }

        model.addAttribute("certificateProfile", certificateProfileService.selByAll());
        return "jzweb/knowledgePoints/edit";
    }

    @Log("添加知识点")
    @ResponseBody
    @PostMapping("/save")
    @RequiresPermissions("jzweb:knowledgePoints:add")
    public Result<String> save(KnowledgePointsDO knowledgePoints) {
//        if(knowledgePoints.getName().equals("综合知识点")){
//            return Result.fail("暂不能添加综合知识点！");
//        }else{
            //根据父级id，证书概况id，等级判断是否重复
            KnowledgePointsDO knowledgePointsDO = knowledgePointsService
                    .findOneByKv("pid",knowledgePoints.getPid(),"name", knowledgePoints.getName(), "profile_id", knowledgePoints.getProfileId(), "grade", knowledgePoints.getGrade());
            if (knowledgePointsDO != null) {
                return Result.fail("知识点已存在，请检查！");
            } else {
                knowledgePoints.setCreaterId(this.getUserId());
                knowledgePointsService.insert(knowledgePoints);
            }
       // }

        return Result.ok();
    }

    @Log("修改知识点")
    @ResponseBody

    @RequestMapping("/update")
    @RequiresPermissions("jzweb:knowledgePoints:edit")
    public Result<String> update(KnowledgePointsDO knowledgePoints) {
        //根据父级id，证书概况id，等级判断是否重复
        List<KnowledgePointsDO> knowledgePointsDOs = knowledgePointsService
                .findByKv("pid",knowledgePoints.getPid(),"name", knowledgePoints.getName(),  "profile_id", knowledgePoints.getProfileId(), "grade", knowledgePoints.getGrade());
        if (knowledgePointsDOs != null && knowledgePointsDOs.size() > 0) {
            if(knowledgePointsDOs.size() == 1){
                if(knowledgePointsDOs.get(0).getId().equals(knowledgePoints.getId())){
                    boolean update = knowledgePointsService.updateById(knowledgePoints);
                    return update ? Result.ok() : Result.fail();
                }
            }
            return Result.fail("知识点已存在，请检查！");
        } else {
            boolean update = knowledgePointsService.updateById(knowledgePoints);
            return update ? Result.ok() : Result.fail();
        }

    }

    @Log("删除知识点")
    @PostMapping("/remove")
    @ResponseBody
    @RequiresPermissions("jzweb:knowledgePoints:remove")
    public Result<String> remove(Long id) {
        KnowledgePointsDO knowledgePointsDO = knowledgePointsService.selectById(id);
        if(knowledgePointsDO.getName().equals("综合知识点")){
            CertificateProfileDO certificateProfileDO = certificateProfileService.selectById(knowledgePointsDO.getProfileId());
            if(certificateProfileDO != null){
                return Result.fail("综合知识点暂不能删除！");
            }
        }
        knowledgePointsService.deleteById(id);
        return Result.ok();
    }

    @Log("批量删除知识点")
    @PostMapping("/batchRemove")
    @ResponseBody
    @RequiresPermissions("jzweb:knowledgePoints:batchRemove")
    public Result<String> remove(@RequestParam("ids[]") Long[] ids) {
        for(Long id : ids){
            KnowledgePointsDO knowledgePointsDO = knowledgePointsService.selectById(id);
            if(knowledgePointsDO.getName().equals("综合知识点")){
                CertificateProfileDO certificateProfileDO = certificateProfileService.selectById(knowledgePointsDO.getProfileId());
                if(certificateProfileDO != null){
                    return Result.fail("综合知识点暂不能删除！");
                }
            }
        }
        knowledgePointsService.deleteBatchIds(Arrays.asList(ids));
        return Result.ok();
    }

    @GetMapping("/tree")
    @ResponseBody
    public Tree<KnowledgePointsDO> tree() {
        Tree<KnowledgePointsDO> tree = new Tree<KnowledgePointsDO>();
        tree = knowledgePointsService.getTree();
        return tree;
    }

    @GetMapping("/treeView")
    String treeView() {
        return "jzweb/knowledgePoints/knowTree";
    }

}
